package com.ctu.gzero.edit
{
	import com.ctu.gzero.elements.DOMElement;
	import com.ctu.gzero.elements.ParaElement;
	import com.ctu.gzero.elements.RunElement;
	import com.ctu.gzero.elements.TxtRun;
		
	public class ParaEdit
	{
		public function ParaEdit()
		{
			super();
		}
		
		public static function splitTextRun(textRun:TxtRun, absolutePosition:int, textLength:int) : TxtRun
        {
            var _textRun:TxtRun;
            var _textRunStart:int = textRun.absoluteStart;
            var _textRunTextLength:int = textRun.textLength;
                        
            if(absolutePosition == _textRunStart)
            {
            	
            }
            
            if(textLength == _textRunTextLength)
            {
            	return textRun;
            }
            
            var offset:int = absoluteStart - _textRunStart;
            if(offset > 0)
            {
            	_textRun = textRun.splitAtPosition(offset);
            	if(offset + textLength < _textRunTextLength)
            	{
            		_textRun.splitAtPosition(textLength);
            	}
            }
            else 
            {
            	textRun.splitAtPosition(textLength);
            	_textRun = textRun;
            }
            
            return _textRun;
        }
        
        public static function setTextStyle(dom:DOMElement, absoluteStart:int, absoluteEnd:int, format:ICharacterFormat):void
        {
            var para:ParaElement;
            var beginIndex:int = absoluteStart;
            
            while (beginIndex < absoluteEnd)
            {                
                para = dom.findParaAtPosition(beginIndex);
                beginIndex = setCharacterFormat(para, format, beginIndex, absoluteEnd - beginIndex);
            }
            return;
        }
        
        public static function setCharacterFormat(para:ParaElement, format:ICharacterFormat, absoluteStart:int, len:int):int
        {
        	var _textRun:TxtRun;
        	var _textRunAbsoluteStart:int;
        	var _offset:int;
        	
        	if(!format.isEqual(para.format))
        	{
        		_textRun = para.getTextRunByAbsolute(absoluteStart);        		
        		_textRunAbsoluteStart = _textRun.absoluteStart;
        		
        		_offset = absoluteStart - _textRunAbsoluteStart;        		
        		
        		if(_textRun is TxtRun)
        		{
        			if(_offset + len > _textRun.textLength)
	        		{
	        			len = _textRun.textLength - _offset;
	        		}
        			_textRun = splitTextRun(_textRun, absoluteStart, len);
        		}
        		else 
        		{
        			_textRun = null;
        			len =
        		}
        		
        		if(_textRun != null && format !=null)
        		{
        			_textRun.format = format;
        		}
        		return absoluteStart + len;
        		
        		
        	} 
        	len = para.textLength;
        	return len + _paraAbsoluteStart;
        	
        	var _loc_6:ParagraphElement;
            var _loc_7:int;
            var _loc_8:int;
            var _loc_9:FlowLeafElement;
            var _loc_5:* = param1.getAbsoluteStart();
           
            if (!TextLayoutFormat.isEqual(ITextLayoutFormat(param2), param1.textLayoutFormat))
            {
                _loc_6 = param1.getParagraph();
                _loc_7 = _loc_6.getAbsoluteStart();
                _loc_8 = param3 - _loc_5;
                if (_loc_8 + param4 > param1.textLength)
                {
                    param4 = param1.textLength - _loc_8;
                }// end if
                if (param1 is SpanElement)
                {
                    _loc_9 = splitForChange(SpanElement(param1), param3, param4);
                }
                else
                {
                    _loc_9 = param1;
                }// end else if
                if (param2 is ITextLayoutFormat)
                {
                    _loc_9.textLayoutFormat = ITextLayoutFormat(param2);
                }
                else
                {
                    _loc_9.setCoreStylesInternal(param2);
                }// end else if
                return param3 + param4;
            }// end if
            param4 = param1.textLength;
            return _loc_5 + param4;
        }
	}
}